const Router = require('koa-router');
const tool = require("../../tools/tool");
const router = new Router();
const {db, Op, QueryTypes} = require("../../models/db");
// 核销单 获取 预收列表，抵扣列表
router.post('/bill/getYsBillList', async ctx => {
    let user = ctx.user
    let body = ctx.request.body
    let tenantId = user.tenantId;
    let buId = body.buId
    let otherType = body.otherType
    if(!otherType) return ctx.err('请选择类型')
    if(!buId) return ctx.err('往来单位')
    if(otherType === '预收冲应收') {
        let sql1 = `select id billId,totalAmount,billNo srcBillNo,billType srcBillType,transType srcTransType,transTypeName srcTransTypeName,billDate,arrears,hxAmount,leftAmount,0 nowCheck from invoice where tenantId=${tenantId} and buId=${buId} and transType=153407 and billState='accounted' and leftAmount<>0 `
        let sql2 = `select id billId,totalAmount,billNo srcBillNo,billType srcBillType,transType srcTransType,transTypeName srcTransTypeName,billDate,arrears,hxAmount,leftAmount,0 nowCheck from invoice where tenantId=${tenantId} and buId=${buId} and transType=150601 and billState='accounted' and leftAmount<>0 `
        let yusList = await db.query(sql1, {type: QueryTypes.SELECT})
        let infos = await db.query(sql2, {type: QueryTypes.SELECT})

        return ctx.suc('', {
            hxList:yusList,
            billList: infos
        })
    }
    if(otherType === '预付冲应付') {
        let sql1 = `select id billId,totalAmount,billNo srcBillNo,billType srcBillType,transType srcTransType,transTypeName srcTransTypeName,billDate,arrears,hxAmount,leftAmount,0 nowCheck from invoice where tenantId=${tenantId} and buId=${buId} and transType=153406 and billState='accounted' and leftAmount<>0 `
        let sql2 = `select id billId,totalAmount,billNo srcBillNo,billType srcBillType,transType srcTransType,transTypeName srcTransTypeName,billDate,arrears,hxAmount,leftAmount,0 nowCheck from invoice where tenantId=${tenantId} and buId=${buId} and transType=150501 and billState='accounted' and leftAmount<>0 `
        let yusList = await db.query(sql1, {type: QueryTypes.SELECT})
        let infos = await db.query(sql2, {type: QueryTypes.SELECT})

        return ctx.suc('', {
            hxList:yusList,
            billList: infos
        })
    }
})

module.exports = router;